package com.wtwd.device.controller;

import com.github.xiaoymin.knife4j.annotations.DynamicParameter;
import com.github.xiaoymin.knife4j.annotations.DynamicParameters;
import com.wtwd.device.constant.RespCode;
import com.wtwd.device.model.entity.DialManagement;
import com.wtwd.device.model.entity.SysUser;
import com.wtwd.device.model.vo.RespEntity;
import com.wtwd.device.service.DialService;
import com.wtwd.device.utils.GetCurrentSysUserInfoUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;

/**
 * @author mjy
 * @date 2021/08/07
 * @description
 */
@Api(tags = "表盘管理")
@RestController
@RequestMapping("/ordinaryDial")
public class DialController {

    private static final Logger logger = LoggerFactory.getLogger(DialController.class);

    @Autowired
    private DialService dialService;
    @Autowired
    private GetCurrentSysUserInfoUtils getCurrentSysUserInfoUtils;


    /**
     * 表盘管理列表
     *
     * @param currentPage
     * @param pageSize
     * @param dialName
     * @param watchIds
     * @param isShow
     * @param type
     * @return
     */
    @ApiOperation(value = "分页获取表盘管理列表", notes = "")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "currentPage", value = "当前页", required = true, dataTypeClass = Integer.class)
            , @ApiImplicitParam(name = "pageSize", value = "页面大小", required = true, dataTypeClass = Integer.class)
            , @ApiImplicitParam(name = "dialName", value = "表盘名称", dataTypeClass = String.class)
            , @ApiImplicitParam(name = "watchIds", value = "手表id", dataTypeClass = Integer.class)
            , @ApiImplicitParam(name = "isShow", value = "是否展示0:不展示,1：展示", dataTypeClass = String.class)
            , @ApiImplicitParam(name = "type", value = "表盘类型0:圆形,1:方形",dataTypeClass = String.class)
    })
    @GetMapping("/getDialList")
    public RespEntity getDialList(
            @RequestParam(value = "currentPage", defaultValue = "1") Integer currentPage,
            @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
            @RequestParam(value = "dialName") String dialName,
            @RequestParam(value = "watchIds") String watchIds,
            @RequestParam(value = "isShow") Integer isShow,
            @RequestParam(value = "type") Integer type
    ) {
        logger.info("getDialList param" + currentPage, pageSize, dialName, watchIds, isShow, type);
        SysUser sysUser = getCurrentSysUserInfoUtils.getCurrentSysUserInfo();
        if (sysUser == null) {
            return new RespEntity(RespCode.TOKEN_UNAUTHORIZED);
        }
        //构建查询Map
        HashMap<Object, Object> searchMap = new HashMap<>();
        searchMap.put("dialName", dialName);
        searchMap.put("watchIds", watchIds);
        searchMap.put("isShow", isShow);
        searchMap.put("type", type);
        return dialService.getDialListByConditions(currentPage, pageSize, searchMap, sysUser);
    }

    /**
     * 添加表盘
     *
     * @param dialManagement
     * @return
     */
    @ApiOperation(value = "添加表盘",notes = "")
    @DynamicParameters(name = "addDial", properties = {
             @DynamicParameter(name = "dialName", value = "表盘名", dataTypeClass = String.class, required = true)
            ,@DynamicParameter(name = "shape", value = "表盘类型,表盘形状类型 0：圆形1：方形", dataTypeClass = Integer.class, required = true)
            ,@DynamicParameter(name = "isShow", value = "是否展示", dataTypeClass = Integer.class, required = true)
            ,@DynamicParameter(name = "resolution", value = "是否激活", dataTypeClass = Integer.class, required = true)
            ,@DynamicParameter(name = "previewImg", value = "图片地址", dataTypeClass = String.class, required = true)
            ,@DynamicParameter(name = "watchIds", value = "手表id组", dataTypeClass = String.class, required = true)
            ,@DynamicParameter(name = "resolution", value = "预览图分辨率240*240", dataTypeClass = String.class, required = true)
            ,@DynamicParameter(name = "dialId", value = "表盘id", dataTypeClass = String.class, required = true)
    })
    @PostMapping("/addDial")
    public RespEntity addDial(@RequestBody DialManagement dialManagement) {
        logger.info("addDial param" + dialManagement.toString());
        SysUser sysUser = getCurrentSysUserInfoUtils.getCurrentSysUserInfo();
        if (sysUser == null) {
            return new RespEntity(RespCode.TOKEN_UNAUTHORIZED);
        }
        return dialService.addDial(dialManagement, sysUser);
    }


    /**
     * 编辑表盘
     *
     * @param dialManagement
     * @return
     */
    @ApiOperation(value = "编辑表盘",notes = "")
    @DynamicParameters(name = "updateDial", properties = {
            @DynamicParameter(name = "id", value = "表盘名", dataTypeClass = Integer.class, required = true)
            ,@DynamicParameter(name = "dialName", value = "表盘名", dataTypeClass = String.class, required = true)
            ,@DynamicParameter(name = "shape", value = "表盘类型,表盘形状类型 0：圆形1：方形", dataTypeClass = Integer.class, required = true)
            ,@DynamicParameter(name = "isShow", value = "是否展示", dataTypeClass = Integer.class, required = true)
            ,@DynamicParameter(name = "resolution", value = "是否激活", dataTypeClass = Integer.class, required = true)
            ,@DynamicParameter(name = "previewImg", value = "图片地址", dataTypeClass = String.class, required = true)
            ,@DynamicParameter(name = "watchIds", value = "手表id组", dataTypeClass = String.class, required = true)
            ,@DynamicParameter(name = "resolution", value = "预览图分辨率240*240", dataTypeClass = String.class, required = true)
            ,@DynamicParameter(name = "dialId", value = "表盘id", dataTypeClass = String.class, required = true)
    })
    @PutMapping("/updateDial")
    public RespEntity updateDial(@RequestBody DialManagement dialManagement) {
        logger.info("updateDial param" + dialManagement.toString());
        SysUser sysUser = getCurrentSysUserInfoUtils.getCurrentSysUserInfo();
        if (sysUser == null) {
            return new RespEntity(RespCode.TOKEN_UNAUTHORIZED);
        }
        return dialService.updateDial(dialManagement, sysUser);
    }

    /**
     * 删除表盘
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "删除表盘")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "用户id", required = true, dataTypeClass = Integer.class)
    })
    @DeleteMapping("deleteDial/{id}")
    public RespEntity deleteDial(@PathVariable("id") Integer id) {
        logger.info("deleteDial param" + id);
        return dialService.deleteDial(id);
    }


}
